package com.example.final_project.Comm.frag;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ejlchina.okhttps.OkHttps;
import com.example.final_project.Comm.adapter.CommAdapter;
import com.example.final_project.Comm.entry.CommContent;
import com.example.final_project.Comm.entry.CommGuild;
import com.example.final_project.Comm.entry.CommDatas;
import com.example.final_project.R;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.List;

public class CommFragment extends Fragment {

    private Context context;
    private RecyclerView comm;
    private int pageNow=1;
    private int pageSize=3;
    public static final int COMM=0;
    public static final int COMMLOADING=1;
    public static final int COMMREFRESH=2;
    private CommDatas commDatas;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_comm, container, false);

        context = CommFragment.this.getContext();
        comm = view.findViewById(R.id.comm_position);
        commDatas = new CommDatas();
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context,1);
        comm.setLayoutManager(gridLayoutManager);
        RefreshLayout refreshLayout = view.findViewById(R.id.refreshLayout);
        refreshLayout.setRefreshHeader(new ClassicsHeader(context));
        refreshLayout.setRefreshFooter(new ClassicsFooter(context));
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNow=0;
                if(pageNow < 2){
                    pageNow++;
                }else{
                    pageNow=1;
                }
                RefreshData();
                refreshLayout.finishRefresh(true);//传入false表示刷新失败
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                if(pageNow < 2){
                    pageNow++;
                }else{
                    pageNow=0;
                }
                LoadData();
                refreshlayout.finishLoadMore(true);//传入false表示加载失败
            }
        });
        getData();

        return view;

    }

    private Handler handler = new Handler(Looper.getMainLooper()){

        private CommAdapter commAdapter;

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case COMM:
                    CommDatas datas = (CommDatas)msg.obj;
                    commAdapter = new CommAdapter(context,datas);
                    comm.setAdapter(commAdapter);
                    break;
                case COMMLOADING:
                    List<CommContent> list = (List<CommContent>)msg.obj;
                    commAdapter.getData(list);
                    break;
                case COMMREFRESH:
                    CommDatas data = (CommDatas)msg.obj;
                    List<CommContent> commContent = data.getContents();
                    CommContent commContent1 = commContent.get(0);
                    String name = commContent1.getName();
                    commAdapter = new CommAdapter(context,data);
                    comm.setAdapter(commAdapter);
                    break;
            }


        }
    };

    //获取相关数据
    public void getData(){
        OkHttps.async("http://10.150.11.9:8086/chengcar/commguild")
                .bind(CommFragment.this)
                .setOnResList(CommGuild.class,guilds ->{
                    commDatas.setGuilds(guilds);
                })
                .get();
        OkHttps.async("http://10.150.11.9:8086/chengcar/findContentByPage")
                .bind(context)
                .addUrlPara("pageNow",pageNow)
                .addUrlPara("pageSize",pageSize)
                .setOnResList(CommContent.class,context ->{
                    commDatas.setContents(context);
                    Message message = Message.obtain();
                    message.what = COMM;
                    message.obj = commDatas;
                    handler.sendMessage(message);
                })
                .get();

    }

    public void LoadData(){
        OkHttps.async("http://10.150.11.9:8086/chengcar/findContentByPage")
                .bind(context)
                .addUrlPara("pageNow",pageNow)
                .addUrlPara("pageSize",pageSize)
                .setOnResList(CommContent.class,ccomm ->{
                    Message message = Message.obtain();
                    message.what=COMMLOADING;
                    message.obj=ccomm;
                    handler.sendMessage(message);
                })
                .get();
    }

    public void RefreshData(){
        OkHttps.async("http://10.150.11.9:8086/chengcar/refreshcontent/")
                .addUrlPara("pageNow",pageNow)
                .addUrlPara("pageSize",pageSize)
                .setOnResList(CommContent.class,ccomm ->{
                    commDatas.setContents(ccomm);
                    Message message = Message.obtain();
                    message.what=COMMREFRESH;
                    message.obj=commDatas;
                    handler.sendMessage(message);
                })
                .get();
    }

}